package org.flexharmony.harmonizer;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.flexharmony.harmonizer.config.HarmonyConfiguration;

public class ClassFilesHarmonizer implements FilenameFilter
{
  private Harmonizer classHarmonzier;
  private int totalClassesHarmonized;
  private int totalClassesUpdated;
  
  public ClassFilesHarmonizer(HarmonyConfiguration configuration)
  {
    classHarmonzier = new Harmonizer(configuration);
  }
  
  public void harmonize(File... files) throws IOException
  {
    List<File> filesList = new ArrayList<File>();
    for (File file : files)
    {
      filesList.add(file);
    }
    harmonize(filesList);
  }
  
  public void harmonize(List<File> files) throws IOException
  {
    for (File file : files)
    {
      if ((file.isFile() && file.getName().endsWith(".class")))
      {
        ++totalClassesHarmonized;
        if (classHarmonzier.harmonize(file))
        {
          ++totalClassesUpdated;
        }
      }
      else
      {
        File[] classFiles = file.listFiles(this);
        if (classFiles != null)
        {
          harmonize(classFiles);
        }
      }
    }
    classHarmonzier.writeUpdates();
  }

  public boolean accept(File dir, String name)
  {
    return name.endsWith(".class");
  }

  public int getTotalClassesHarmonized()
  {
    return totalClassesHarmonized;
  }

  public int getTotalClassesUpdated()
  {
    return totalClassesUpdated;
  }
  
  public void reset()
  {
    totalClassesHarmonized = 0;
    totalClassesUpdated = 0;
  }
}
